@using RatScanner.TarkovDev.GraphQL
@using Color = MudBlazor.Color
@using System.Diagnostics;

<MudPaper Class="pa-2" Elevation="3" Style="">
    <MudStack Row="true" Justify="Justify.SpaceBetween" Style="height: 6vh;">
        <!--Left side of the search result item-->
        <MudStack Row="true" Justify="Justify.FlexStart" Class="flex-1" Style="">
            <MudPaper Style="aspect-ratio : 1 / 1;" Outlined="true">
                <MudImage Src="@Item.BaseImageLink"
                          Style="width: 100%; height: 100%; object-fit: contain;" />
            </MudPaper>

            <MudStack Justify="Justify.SpaceEvenly" AlignItems="AlignItems.End" Style="width: 4vw;">
                <MudChip OnClose="() => { }" CloseIcon="@Icons.Material.Filled.Checklist" Size="Size.Small"
                         Variant="Variant.Outlined" Color="@(Item.GetTaskRemaining().Item1 > 0 ? Color.Success : Color.Default)">
                    @Item.GetTaskRemaining().Item1.ToShortString()
                </MudChip>
                <MudChip OnClose="() => { }" CloseIcon="@Icons.Material.Filled.House" Size="Size.Small"
                         Variant="Variant.Outlined" Color="@(Item.GetHideoutRemaining() > 0 ? Color.Success : Color.Default)">
                    @Item.GetHideoutRemaining().ToShortString()
                </MudChip>
            </MudStack>

            <MudDivider Vertical="true" FlexItem="true" />

            <MudStack Justify="Justify.SpaceEvenly" AlignItems="AlignItems.Start">
                <MudText Class="ellipsis" Typo="Typo.h6" Style="max-width: 16vw">
                    @Item.Name
                </MudText>
                <MudText Align="Align.Center">
                    @Item.ShortName
                    <MudIconButton Icon="@Constants.Icon.TarkovDev" Size="Size.Small" OnClick=@(() => OpenURL($"https://tarkov.dev/item/{Item.Id}")) />
                    <MudIconButton Icon="@Icons.Material.Filled.OpenInNew" Size="Size.Small" OnClick=@(() => OpenURL(Item.WikiLink)) />
                </MudText>
            </MudStack>
        </MudStack>

        <!--Right side of the search result item-->
        <MudStack Row="true" Justify="Justify.FlexEnd">
            <MudStack Justify="Justify.SpaceEvenly" AlignItems="AlignItems.End" Style="width: 8vw" Spacing="0">
                <MudText Typo="Typo.h6">
                    @Item.Avg24HPrice.AsRubs()
                </MudText>
                <MudText>
                    @Item.GetAvg24hMarketPricePerSlot().AsRubs()
                </MudText>
            </MudStack>

            <MudDivider Vertical="true" FlexItem="true" />

            <MudStack Justify="Justify.SpaceEvenly" AlignItems="AlignItems.Center" Style="width: 3vw" Spacing="0">
                @{
                    var bestTrader = Item.GetBestTraderOffer();
                    var traderImage = Item.GetBestTraderOfferVendor()?.Trader?.ImageLink;
                }
                <MudAvatar Image="@traderImage"
                           Size="Size.Medium" Style="float: left;" Square="true" Variant="Variant.Outlined" />
                <MudText Typo="Typo.subtitle2" Style="float: right;">@bestTrader?.PriceRub.AsRubs()</MudText>
            </MudStack>
        </MudStack>
    </MudStack>

    @if (IsExpanded && Item.Properties is ItemPropertiesAmmo ammoProp) {
        <MudDivider Vertical="false" DividerType="DividerType.Middle" Class="mx-0 my-2" />

        <MudStack Row="false">
            <MudTable T="Item" Items="@Item.GetAmmoOfSameCaliber()" Dense="true" FixedHeader="true" Elevation="0" Striped="true" Style="max-height: 300px; overflow-y: auto;" RowStyleFunc="@((ctx, _) => ctx.Id == Item.Id ? "background: #FFFF8040;" : "")">
                <HeaderContent>
                    <MudTh Style="">Name</MudTh>
                    <MudTh Style="text-align:right"><MudTableSortLabel SortBy="new Func<Item, object>(x=>x.Avg24HPrice)">Price</MudTableSortLabel></MudTh>
                    <MudTh Style="text-align:right"><MudTableSortLabel SortBy="new Func<Item, object>(x=>(x.Properties as ItemPropertiesAmmo).Damage)">Damage</MudTableSortLabel></MudTh>
                    <MudTh Style="text-align:right"><MudTableSortLabel InitialDirection="SortDirection.Ascending" SortBy="new Func<Item, object>(x=>(x.Properties as ItemPropertiesAmmo).PenetrationPower)">Pen</MudTableSortLabel></MudTh>
                    <MudTh Style="text-align:right">Frag %</MudTh>
                    <MudTh Style="text-align:center">Armor Pen</MudTh>
                </HeaderContent>
                <RowTemplate>
                    @if (context.Properties is not ItemPropertiesAmmo ammo) return;
                    <MudTd Class="my-0 py-0">
                        <MudStack Row="true" AlignItems="AlignItems.Center">
                            <MudImage Src="@context.BaseImageLink" Style="width: 32px; height: 32px;" />
                            <MudText Style="">@context.ShortName</MudText>
                        </MudStack>
                    </MudTd>
                    <MudTd Style="text-align:right">@context.Avg24HPrice.AsRubs()</MudTd>
                    <MudTd Style="text-align:right">@ammo.Damage</MudTd>
                    <MudTd Style="text-align:right">@ammo.PenetrationPower</MudTd>
                    <MudTd Style="text-align:right">@((int)((ammo.FragmentationChance ?? 0m) * 100m))%</MudTd>
                    <MudTd Style="text-align:center">
                        @for (int _i = 0; _i < 6; _i++) {
                            int i = _i;
                            decimal value = Math.Clamp((ammo.PenetrationPower ?? 0m) / 10m - i, 0, 1);
                            int hue = (int)(value * 120m);
                            string style = $"background-color: hsl({hue},100%,40%);";
                            <MudAvatar Size="Size.Small" Square="true" Variant="Variant.Outlined" Style="@style">
                                @(i+1)
                            </MudAvatar>
                        }
                    </MudTd>
                </RowTemplate>
            </MudTable>
        </MudStack>
    }
</MudPaper>

<style>
    .ellipsis {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        min-width: 0;
    }
</style>

@code {
    [Parameter]
    public required Item Item { get; set; }

    [Parameter]
    public bool IsExpanded { get; set; }

    private void OpenURL(string? url) {
        if (string.IsNullOrEmpty(url)) return;
        var psi = new ProcessStartInfo {
            FileName = url,
            UseShellExecute = true,
        };
        Process.Start(psi);
    }
}
